package com.stockz.equity.charting.view.widget;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.event.ChangeTimeUnitEvent;
import com.stockz.equity.charting.event.EventPublisher;
import com.stockz.equity.charting.util.Constant.TIME_UNIT;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.charting.view.toolbar.ViewToolbar;
import com.stockz.equity.pattern.Child;

@Component
public class TimeUnitCombobox extends JComboBox implements Child,Ordered {
	private static final long serialVersionUID = 890428682284599991L;

	@SuppressWarnings("unchecked")
	private List<Class> parentList = new ArrayList<Class>(1);
	@Autowired StockChartView stockChartView;
	
	public TimeUnitCombobox(){
		super();
		parentList.add(ViewToolbar.class);
		setModel(new DefaultComboBoxModel(TIME_UNIT.values()));
		setSelectedItem(TIME_UNIT.DAILY);
		addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				
				stockChartView.getSelectedStockChart()
				.getStockChartPanel().getStockChartPanelState()
				.setCurrentTimeUnit((TIME_UNIT)getSelectedItem());

				EventPublisher.publishEvent(
						new ChangeTimeUnitEvent(
								stockChartView.getSelectedStockChart()));
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Class> getParents() {
		return parentList;
	}

	@Override
	public int getOrder() {
		return 0;
	}
}
